import axios, { ResDataType } from "./ajax";

type SearchOption = {
  keyword: string;
  isStar: boolean;
  isDeleted: boolean;
  page: number;
  pageSize: number;
};

export async function getQuestionServices(id: string): Promise<ResDataType> {
  const url = "/api/question/" + id;
  const data = (await axios.get(url)) as ResDataType;
  return data;
}

export async function createQuestionServices(): Promise<ResDataType> {
  const url = "/api/question";
  const data = (await axios.post(url)) as ResDataType;
  return data;
}

export async function getQuestionListServices(
  opt: Partial<SearchOption> = {}
): Promise<ResDataType> {
  const url = "/api/question";
  const data = (await axios.get(url, { params: opt })) as ResDataType;
  return data;
}

//更新单个问卷
export async function updateQuestionService(
  id: number,
  opt: { [key: string]: any }
): Promise<ResDataType> {
  const url = "/api/question/" + id;
  const data = (await axios.patch(url, opt)) as ResDataType;
  return data;
}

//复制问卷
export async function duplicateQuestionService(
  id: number
): Promise<ResDataType> {
  const url = "/api/question/duplicate/" + id;
  const data = (await axios.post(url)) as ResDataType;
  return data;
}

//批量彻底删除
export async function deleteQuestionsService(
  ids: number[]
): Promise<ResDataType> {
  const url = "/api/question";
  const data = (await axios({
    url,
    method: "delete",
    data: { ids },
  })) as ResDataType;
  return data;
}
